package com.geotmt.billingcenter.modules.cost.mapper.provider;

import com.alibaba.druid.util.StringUtils;
import com.geotmt.billingcenter.common.vo.params.ParamsCost;
import org.apache.ibatis.annotations.Param;

/**
 * @Description where 条件
 * @Author      yanghanwei
 * @Mail        yanghanwei@geotmt.com
 * @Date        2019/3/14 10:08
 * @Version     v1
 **/
public abstract class WhereProvider {

    /**
     * 成本的 where 语句
     * @param sql
     * @param obj
     */
    public void whereCost(StringBuffer sql, String baseAlias, @Param(value = "obj") ParamsCost obj){
        sql.append(" AND ").append(baseAlias).append(".status <>2");
        if(null != obj.getDsId()){
            sql.append(" AND ").append(baseAlias).append(".ds_id=#{dsId}");
        }
        if(null != obj.getOutInterfaceId()){
            sql.append(" AND ").append(baseAlias).append(".out_interface_id=#{outInterfaceId }");
        }
        if(null != obj.getFeeFlag()){
            sql.append(" AND ").append(baseAlias).append(".fee_flag=#{feeFlag}");
        }
        String interfaceIds = obj.getOutInterfaceIds();
        if(!StringUtils.isEmpty(interfaceIds)){
            String ids = interfaceIds.substring(0, interfaceIds.length() - 1);
            //写死的接口表别名
            sql.append(" AND ").append("inter").append(".id IN(").append(ids).append(")");
        }
        if(null != obj.getEffectStartTime() && null != obj.getEffectEndTime()){
            sql.append(" AND (")
                    .append("(#{effectStartTime}>=").append(baseAlias).append(".effect_start_time AND #{effectStartTime} <=").append(baseAlias).append(".effect_end_time)")
                    .append("OR (#{effectEndTime}>=").append(baseAlias).append(".effect_start_time AND #{effectEndTime} <=").append(baseAlias).append(".effect_end_time)")
                    .append("OR (#{effectStartTime}<=").append(baseAlias).append(".effect_start_time AND #{effectEndTime}>= ").append(baseAlias).append(".effect_end_time)")
                    .append(")");
        }
    }

    /**
     * For Inter
     * @param sql
     * @param obj
     */
    public void whereCostListForInter(StringBuffer sql, @Param(value = "obj") ParamsCost obj){
        if(null != obj.getDsId()){
            sql.append(" AND iods.ds_id=#{dsId}");
        }
    }

    /**
     * base表的关联条件
     * @param sql
     * @param baseAlias
     * @param obj
     */
    public void whereConsumeListAllForCurr(StringBuffer sql, String baseAlias, @Param(value = "obj") ParamsCost obj){
        sql.append(" AND ").append(baseAlias).append(".status <>2 ");
        if(null != obj.getDsId()){
            sql.append(" AND ").append(baseAlias).append(".ds_id=#{dsId}");
        }
    }

    /**
     * 回显list
     * @param sql
     * @param baseAlias
     * @param obj
     */
    public void whereCostListAll(StringBuffer sql, String baseAlias, @Param(value = "obj") ParamsCost obj){
        String interfaceIds = obj.getOutInterfaceIds();
        if(!StringUtils.isEmpty(interfaceIds)){
            String ids = interfaceIds.substring(0, interfaceIds.length() - 1);
            //写死的 接口表别名
            sql.append(" AND ").append(baseAlias).append(".id IN(").append(ids).append(")");
        }
    }
}
